﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace BusinessLogicLayer
{
   public class RetrivalListManager
    {
        InventorySoreDataContext context;
        public RetrivalListManager()
        {
            context = new InventorySoreDataContext();
        }
        public IQueryable getRetrivalList()
        {
            //var query = (from retDetail in context.RetrivalListDetails
            //             where retDetail.RetrivalList.Status == "Approved" && retDetail.Remark == "Processing"
            //             group retDetail by
            //             new
            //             {
            //                 retDetail.ItemCode,
            //                 retDetail.Item.Category.CategoryName,
            //                 retDetail.Item.Description,
            //                 retDetail.Item.Stock,
            //                 retDetail.NeededQty,
            //                 retDetail.Item.Bin
            //             }
            //                 into retrivalList
            //                 select new
            //                 {
            //                     ItemCode = retrivalList.Key.ItemCode,
            //                     Category = retrivalList.Key.CategoryName,
            //                     Description = retrivalList.Key.Description,
            //                     NeededQuantity = retrivalList.Sum(s => s.NeededQty),
            //                     AcutalQuantity = retrivalList.Key.Stock,
            //                     Bin = retrivalList.Key.Bin,
            //                 }).Distinct();


            var query = (from retDetail in context.RetrivalListDetails
                         where retDetail.RetrivalList.Status == "Approved" && retDetail.Remark == "Processing"
                         group retDetail by retDetail.ItemCode into total
                         from retDetail in context.RetrivalListDetails
                         where total.Key.Equals(retDetail.ItemCode) 
                         select new
                         {
                             ItemCode = retDetail.ItemCode,
                             Category = retDetail.Item.Category.CategoryName,
                             Description = retDetail.Item.Description,
                             NeededQuantity = total.Sum(s => s.NeededQty),
                             AcutalQuantity = retDetail.Item.Stock,
                             Bin = retDetail.Item.Bin,
                         }).Distinct();
            return query;
           
        }
        public List<RetrivalDetails> getRetrivalDetails(string itemCode)
        {
            var query = (from retrivaldet in context.RetrivalListDetails where retrivaldet.ItemCode == itemCode && retrivaldet.RetrivalList.Status=="Approved"
                         orderby retrivaldet.RetrivalList.Department.PriorityID,retrivaldet.RetrivalList.LastModified ascending
                        select new RetrivalDetails{
                            ItemCode=retrivaldet.ItemCode,
                            DepartmentName= retrivaldet.RetrivalList.Department.DepartmentName, 
                            NeededQuantity=Convert.ToInt32( retrivaldet.NeededQty),
                            ActualQuantity= Convert.ToInt32(retrivaldet.ActualQty),
                            DepartmentCode=retrivaldet.RetrivalList.Department.DepartmentCode,
                           RetrivalListID=retrivaldet.RetrivalListID,
                           
                        });
            Item item = new ItemFacade().FindItemByID(itemCode);
            List<RetrivalDetails> retrivalDetails = new List<RetrivalDetails>();
            foreach (RetrivalDetails retDetail in query)
            {
                
                int allocateQty = item.Stock - retDetail.NeededQuantity;
                if (allocateQty < 0)
                {//to check if stock is lower than ordered stock
                    allocateQty = retDetail.NeededQuantity + allocateQty;
                    retDetail.ActualQuantity = allocateQty;
                }
                else
                {
                    retDetail.ActualQuantity = retDetail.NeededQuantity;
                }
                item.Stock = item.Stock - retDetail.ActualQuantity;
                retrivalDetails.Add(retDetail);
 
            }
            return retrivalDetails;

        }
       
        public void UpdateQuantity(string retrivalListID,string itemCode,int actualQuantity)
        {
            
            RetrivalListDetail retDetail = new RetrivalListFacade().FindRetrivalListDetailsByIDAndItemCode(retrivalListID, itemCode);
            retDetail.Remark = "Processed";
            
            context.SubmitChanges();
            //context.
            
        }
        //public static void Main(string[] args)
        //{
        //   new RetrivalListFacade().UpdateActualQty("A001", "E001", 20);
            
        //    Console.WriteLine("Update done!");
        //    Console.ReadLine();
        //}
    }
}
